/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nxlv2.nomad.view.ficheClient;

import com.lowagie.text.Cell;
import com.nxlv2.nomad.businessobjects.Article;
import com.nxlv2.nomad.businessobjects.Client;
import com.nxlv2.nomad.businessobjects.Commande;
import com.nxlv2.nomad.businessobjects.Demande;
import com.nxlv2.nomad.businessobjects.DetailCommande;
import com.nxlv2.nomad.businessobjects.Prix;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.joda.time.DateTime;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;
import com.nxlv2.nomad.accessdb.services.AdresseServices;
import com.nxlv2.nomad.accessdb.services.ClientServices;
import com.nxlv2.nomad.accessdb.services.CommandeServices;
import com.nxlv2.nomad.accessdb.services.ContactServices;
import com.nxlv2.nomad.accessdb.services.DetailCommandeServices;
import com.nxlv2.nomad.businessobjects.Adresse;
import com.nxlv2.nomad.businessobjects.Contact;
import com.nxlv2.nomad.businessobjects.TauxSatisfaction;
import com.nxlv2.nomad.view.Welcome;
import java.awt.Color;
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author Nastasia
 */
public class FicheClient extends javax.swing.JFrame {

    private final Client client;
    private final Welcome welcome;
    private Contact contact;
    private Adresse adresse;
    private Demande demande;

    /**
     *
     * @param cl
     * @param wel
     */
    public FicheClient(Client cl, Welcome wel) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.client = cl;
        this.welcome = wel;
        initData();
    }

    /**
     * Initialisation informations
     */
    public void initData() {
        txtNomClient.setText(client.getNom());
        majContacts();
        majAdresses();
        majDemandes();
        majCommandes();
    }
    // Mise à jour Demandes

    public void majDemandes() {
        List<Demande> demandes = new ArrayList<Demande>();

        ClientServices srvCl = new ClientServices();
        demandes = srvCl.getDemandes(client);
        final DefaultTableModel model = (DefaultTableModel) JTableDemandes.getModel();
        model.getDataVector().removeAllElements();
        // {{{ LUCK... le faite de passer chaque ligne pour le cas de la modif d'une demande
        // permet d'écraser une même demande d'un type différent...
        // tant que les types seront dans le bon ordre... 
        for (Demande demande : demandes) {
//            if ("Commande" != demande.getTypeDemande().getLibelle()) {
            boolean existe = false;
            for (int i = 0; i < JTableDemandes.getRowCount(); i++) {
                Demande dem = (Demande) model.getValueAt(i, 1);
                if (demande.getIdAppli() == dem.getIdAppli()) {
                    model.setValueAt(demande, i, 1);
                    if (1 == demande.getValideDemande()) {
                        model.setValueAt(demande.getTypeDemande().getLibelle() + " validé", i, 2);
                    } else {
                        if (3 == demande.getValideDemande()) {
                            model.setValueAt(demande.getTypeDemande().getLibelle() + " refusé", i, 2);
                        } else {
                            if ("Commande" == demande.getTypeDemande().getLibelle()) {
                                model.removeRow(i);
                                i = i - 1;
                            } else {
                                model.setValueAt(demande.getTypeDemande().getLibelle(), i, 2);
                            }
                        }
                    }
                    existe = true;
                }
            }
            if ("Commande" != demande.getTypeDemande().getLibelle()) {
                if (existe == false) {
                    Object[] obj = new Object[]{"D" + demande.getNumDemande(), demande, demande.getTypeDemande().getLibelle()};
                    model.addRow(obj);
                }
            }
        }
    }
    // Mise à jour Commandes

    public void majCommandes() {
        List<Commande> commandes = new ArrayList<Commande>();
        ClientServices srvCl = new ClientServices();
        commandes = srvCl.getCommandes(client);
        final DefaultTableModel model = (DefaultTableModel) jTableCommandes.getModel();
        model.getDataVector().removeAllElements();
        CommandeServices srvCom = new CommandeServices();
        for (Commande commande : commandes) {
            String libSatisfaction = (0 < srvCom.getSatisfactions(commande).size()) ? srvCom.getSatisfactions(commande).get(0).getTauxSatisfaction().getLibelle() : "";
            boolean existe = false;
            for (int i = 0; i < jTableCommandes.getRowCount(); i++) {
                Commande com = (Commande) model.getValueAt(i, 1);
                if (commande.getIdAppli() == com.getIdAppli()) {
                    model.setValueAt(commande, i, 1);
                    model.setValueAt(commande.getLibelleEtatCommande(), i, 2);
                    model.setValueAt(libSatisfaction, i, 3);
                    existe = true;
                }
            }

            if (existe == false) {
                Object[] obj = new Object[]{"C" + commande.getNumCommande(), commande, commande.getEtatCommande().getLibWeb(), libSatisfaction};
                model.addRow(obj);
            }
        }
    }
    // Mise à jour Contacts 

    public void majContacts() {
        List<Contact> contacts = new ArrayList<>();

        ClientServices srvCl = new ClientServices();
        contacts = srvCl.getContacts(client);
        final DefaultTableModel model = (DefaultTableModel) jTableContacts.getModel();
        jTableContacts.setDefaultRenderer(Object.class, new TableRenderer());
        model.getDataVector().removeAllElements();

        for (Contact contact : contacts) {
            Object[] obj = new Object[]{contact, contact.getPrenom(), contact.getMail(), contact.getTel()};
            model.addRow(obj);
        }

        // Voir pour faire toString de l'objet
    }

    // Mise à jour Adresse 
    public void majAdresses() {
        List<Adresse> adresses = new ArrayList<Adresse>();
        int ligneDefault;

        ClientServices srvCl = new ClientServices();
        adresses = srvCl.getAdresses(client);
        final DefaultTableModel model = (DefaultTableModel) jTableAdresses.getModel();
        jTableAdresses.setDefaultRenderer(Object.class, new TableRenderer());
        model.getDataVector().removeAllElements();

        for (Adresse adresse : adresses) {
            Object[] obj = new Object[]{adresse, adresse.getNomVoie(), adresse.getComplement(),
                adresse.getCodePostal(), adresse.getVille(), adresse.getPays(), adresse.getMail(), adresse.getTel()};
            model.addRow(obj);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane3 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        btnModifierClient = new javax.swing.JButton();
        txtNomClient = new javax.swing.JTextField();
        btnSauvegarderClient = new javax.swing.JButton();
        btnSupprimerClient = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableContacts = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableAdresses = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        btnAjouterAdresse = new javax.swing.JButton();
        btnAjouterContact = new javax.swing.JButton();
        btnModifierContact = new javax.swing.JButton();
        btnSupprimerContact = new javax.swing.JButton();
        btnSupprimerAdresse = new javax.swing.JButton();
        btnModifierAdresse = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        JTableDemandes = new javax.swing.JTable();
        btnAjoutDemande = new javax.swing.JButton();
        jButtonVoirDemande = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jButtonImprimerCommande = new javax.swing.JButton();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTableCommandes = new javax.swing.JTable();
        jButtonNoterCommande = new javax.swing.JButton();
        jButtonVoirCommande = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Fiche client");

        btnModifierClient.setText("Modifier");
        btnModifierClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModifierClientActionPerformed(evt);
            }
        });

        txtNomClient.setEnabled(false);

        btnSauvegarderClient.setText("Sauvegarder");
        btnSauvegarderClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSauvegarderClientActionPerformed(evt);
            }
        });

        btnSupprimerClient.setText("Supprimer");
        btnSupprimerClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSupprimerClientActionPerformed(evt);
            }
        });

        jTableContacts.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nom", "Prénom", "Mail", "Téléphone"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableContacts.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jTableContacts);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Adresses");

        jTableAdresses.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "N° voie", "Nom de voie", "Complément", "Code postal", "Ville", "Pays", "Mail", "Téléhone"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableAdresses.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(jTableAdresses);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Contacts");

        btnAjouterAdresse.setText("Ajouter");
        btnAjouterAdresse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAjouterAdresseActionPerformed(evt);
            }
        });

        btnAjouterContact.setText("Ajouter");
        btnAjouterContact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAjouterContactActionPerformed(evt);
            }
        });

        btnModifierContact.setText("Modifier");
        btnModifierContact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModifierContactActionPerformed(evt);
            }
        });

        btnSupprimerContact.setText("Supprimer");
        btnSupprimerContact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSupprimerContactActionPerformed(evt);
            }
        });

        btnSupprimerAdresse.setText("Supprimer");
        btnSupprimerAdresse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSupprimerAdresseActionPerformed(evt);
            }
        });

        btnModifierAdresse.setText("Modifier");
        btnModifierAdresse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModifierAdresseActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Demandes");

        JTableDemandes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "N° de demande", "Date de demande", "Type de demande"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        JTableDemandes.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane4.setViewportView(JTableDemandes);
        JTableDemandes.getColumnModel().getColumn(1).setPreferredWidth(60);
        JTableDemandes.getColumnModel().getColumn(2).setPreferredWidth(60);

        btnAjoutDemande.setText("Créer");
        btnAjoutDemande.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAjoutDemandeActionPerformed(evt);
            }
        });

        jButtonVoirDemande.setText("Voir");
        jButtonVoirDemande.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVoirDemandeActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Commandes");

        jButtonImprimerCommande.setText("Imprimer");
        jButtonImprimerCommande.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonImprimerCommandeActionPerformed(evt);
            }
        });

        jTableCommandes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "N° Commande", "Date commande", "Etat commande", "Satisfaction"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableCommandes.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane5.setViewportView(jTableCommandes);
        jTableCommandes.getColumnModel().getColumn(0).setResizable(false);
        jTableCommandes.getColumnModel().getColumn(0).setPreferredWidth(80);
        jTableCommandes.getColumnModel().getColumn(1).setResizable(false);
        jTableCommandes.getColumnModel().getColumn(2).setResizable(false);
        jTableCommandes.getColumnModel().getColumn(3).setResizable(false);

        jButtonNoterCommande.setText("Noter");
        jButtonNoterCommande.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNoterCommandeActionPerformed(evt);
            }
        });

        jButtonVoirCommande.setText("Voir");
        jButtonVoirCommande.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVoirCommandeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButtonVoirDemande)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnAjoutDemande))
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 356, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(108, 108, 108)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButtonVoirCommande)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonNoterCommande)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonImprimerCommande))
                            .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtNomClient, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnSauvegarderClient)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btnModifierClient)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnSupprimerClient)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(178, 178, 178)
                                .addComponent(btnModifierContact)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnAjouterContact)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnSupprimerContact))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 466, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnModifierAdresse)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAjouterAdresse)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSupprimerAdresse))
                    .addComponent(jScrollPane2))
                .addGap(0, 0, 0))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnModifierClient)
                            .addComponent(btnSupprimerClient)
                            .addComponent(txtNomClient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSauvegarderClient))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(btnAjouterContact)
                            .addComponent(btnModifierContact)
                            .addComponent(btnSupprimerContact))
                        .addGap(9, 9, 9)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(btnAjouterAdresse)
                            .addComponent(btnSupprimerAdresse)
                            .addComponent(btnModifierAdresse))
                        .addGap(9, 9, 9)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(btnAjoutDemande)
                            .addComponent(jButtonVoirDemande)
                            .addComponent(jLabel4)
                            .addComponent(jButtonImprimerCommande)
                            .addComponent(jButtonNoterCommande)
                            .addComponent(jButtonVoirCommande))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );

        jScrollPane3.setViewportView(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane3)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane3)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSauvegarderClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSauvegarderClientActionPerformed
        // TODO add your handling code here:
        if (!"".equals(txtNomClient.getText())) {
            client.setNom(txtNomClient.getText());
            ClientServices srvCl = new ClientServices();
            srvCl.update(client);
            txtNomClient.setEnabled(false);
            welcome.setListClient();
        }
    }//GEN-LAST:event_btnSauvegarderClientActionPerformed

    private void btnModifierClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModifierClientActionPerformed
        // TODO add your handling code here:
        txtNomClient.setEnabled(true);
    }//GEN-LAST:event_btnModifierClientActionPerformed

    private void btnSupprimerClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSupprimerClientActionPerformed
        // TODO add your handling code here:
        JOptionPane jop = new JOptionPane();
        int option = jop.showConfirmDialog(null,
                "Etes-vous sûr de vouloir supprimer le client ?",
                "Confirmation de suppression",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE);

        if (option == JOptionPane.OK_OPTION) {
            ClientServices srvCl = new ClientServices();
            srvCl.delete(client);
            welcome.setClientSelect(null);
            welcome.setListClient();
            setVisible(false);
            dispose();
        }
    }//GEN-LAST:event_btnSupprimerClientActionPerformed

    private void btnAjouterAdresseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAjouterAdresseActionPerformed
        FormulaireAdresse fa = new FormulaireAdresse(this, client);
        fa.setVisible(true);
    }//GEN-LAST:event_btnAjouterAdresseActionPerformed

    private void btnAjouterContactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAjouterContactActionPerformed
        // TODO add your handling code here:
        FormulaireContact fc = new FormulaireContact(this, client);
        fc.setVisible(true);
    }//GEN-LAST:event_btnAjouterContactActionPerformed

    private void btnModifierContactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModifierContactActionPerformed
        // TODO add your handling code here:
        if (0 > jTableContacts.getSelectedRow()) {
            JOptionPane.showMessageDialog(this, "Vous n'avez pas sélectionné de contact");
        } else {
            int row = jTableContacts.getSelectedRow();
            contact = (Contact) jTableContacts.getValueAt(row, 0);
            FormulaireContact fc = new FormulaireContact(this, client);
            fc.setVisible(true);
        }
    }//GEN-LAST:event_btnModifierContactActionPerformed

    private void btnSupprimerContactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSupprimerContactActionPerformed
        // TODO add your handling code here:
        JOptionPane jop = new JOptionPane();
        if (0 <= jTableContacts.getSelectedRow()) {
            int option = jop.showConfirmDialog(null,
                    "Etes-vous sûr de vouloir supprimer le contact sélectionné ?",
                    "Confirmation de suppression",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE);

            if (option == JOptionPane.OK_OPTION) {
                int row = jTableContacts.getSelectedRow();
                contact = (Contact) jTableContacts.getValueAt(row, 0);
                ContactServices srvCont = new ContactServices();
                srvCont.delete(contact);
                majContacts();
            }
        } else {
            JOptionPane.showMessageDialog(this, "Vous n'avez pas sélectionné de contact");
        }
    }//GEN-LAST:event_btnSupprimerContactActionPerformed

    private void btnModifierAdresseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModifierAdresseActionPerformed
        // TODO add your handling code here:
        if (0 > jTableAdresses.getSelectedRow()) {
            JOptionPane.showMessageDialog(this, "Vous n'avez pas sélectionné d'adresse");
        } else {
            int row = jTableAdresses.getSelectedRow();
            adresse = (Adresse) jTableAdresses.getValueAt(row, 0);
            FormulaireAdresse fa = new FormulaireAdresse(this, client);
            fa.setVisible(true);
        }
    }//GEN-LAST:event_btnModifierAdresseActionPerformed

    private void btnSupprimerAdresseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSupprimerAdresseActionPerformed
        // TODO add your handling code here:
        JOptionPane jop = new JOptionPane();
        if (0 > jTableAdresses.getSelectedRow()) {
            jop.showMessageDialog(this, "Vous n'avez pas sélectionné d'adresse");
        } else {
            int option = jop.showConfirmDialog(null,
                    "Etes-vous sûr de vouloir supprimer l'adresse sélectionnée ?",
                    "Confirmation de suppression",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE);

            if (option == JOptionPane.OK_OPTION) {
                int row = jTableAdresses.getSelectedRow();
                adresse = (Adresse) jTableAdresses.getValueAt(row, 0);
                AdresseServices srvAdr = new AdresseServices();
                srvAdr.delete(adresse);
                majAdresses();
            }
        }
    }//GEN-LAST:event_btnSupprimerAdresseActionPerformed

    private void btnAjoutDemandeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAjoutDemandeActionPerformed
        // TODO add your handling code here:

        FormulaireDemande fd = new FormulaireDemande(this, client, new Demande());
        fd.setVisible(true);
    }//GEN-LAST:event_btnAjoutDemandeActionPerformed

    private void jButtonVoirDemandeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVoirDemandeActionPerformed
        // TODO add your handling code here:
        if (0 > JTableDemandes.getSelectedRow()) {
            JOptionPane.showMessageDialog(this, "Vous n'avez pas sélectionné de demande");
        } else {
            int row = JTableDemandes.getSelectedRow();
            demande = (Demande) JTableDemandes.getValueAt(row, 1);
            FicheDetailDemande dd = new FicheDetailDemande(this, client, demande, null);
            dd.setVisible(true);
        }
    }//GEN-LAST:event_jButtonVoirDemandeActionPerformed

    private void jButtonImprimerCommandeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonImprimerCommandeActionPerformed
        JOptionPane jop = new JOptionPane();
        if (0 > jTableCommandes.getSelectedRow()) {
            jop.showMessageDialog(this, "Vous n'avez pas sélectionné de commande");
        } else {
            int row = jTableCommandes.getSelectedRow();
            Commande commande = (Commande) jTableCommandes.getValueAt(row, 1);
            Document document = new Document(PageSize.A4);
            try {
                double prixTotal = 0;
                PdfWriter.getInstance(document,
                        new FileOutputStream("c:/temp/test.pdf"));
                document.open();
                Table tableau = new Table(5, 2);
                tableau.setAutoFillEmptyCells(true);
                tableau.setPadding(2);

                Cell cell = new Cell("Article");
                addCellHeader(tableau, cell);
                cell = new Cell("Référence");
                addCellHeader(tableau, cell);
                cell = new Cell("Quantité");
                addCellHeader(tableau, cell);
                cell = new Cell("Prix UHT");
                addCellHeader(tableau, cell);
                cell = new Cell("Prix total");
                addCellHeader(tableau, cell);
                tableau.endHeaders();

                List<DetailCommande> detailCommandes = new ArrayList<DetailCommande>();

                CommandeServices srvCom = new CommandeServices();
                DetailCommandeServices srvDet = new DetailCommandeServices();
                detailCommandes = srvCom.getDetailCommandes(commande);

                double prixArticles = 0;

                for (DetailCommande detailCommande : detailCommandes) {
//            System.out.print(demande.getNumDemande());
                    Article article = srvDet.getArticle(detailCommande);
                    Prix prix = srvDet.getPrix(detailCommande);
                    cell = new Cell(article.getLibelle());
                    addCell(tableau, cell);
                    cell = new Cell(article.getReference());
                    addCell(tableau, cell);
                    cell = new Cell(String.valueOf(detailCommande.getQuantite()).toString());
                    addCell(tableau, cell);
                    cell = new Cell(String.valueOf(prix.getPrixUht()).toString());
                    addCell(tableau, cell);
                    prixArticles = prix.getPrixUht() * (double)detailCommande.getQuantite();
                    prixArticles=((double)Math.round(prixArticles * 100) / 100);
                    cell = new Cell(String.valueOf(prixArticles).toString());
                    addCell(tableau, cell);
                    prixTotal = prixTotal + prixArticles;
                }
                Paragraph paragraph = new Paragraph("Facture n° " + commande.getNumCommande(), new Font(Font.COURIER, 24, Font.BOLD, Color.black));
                paragraph.setAlignment(Element.ALIGN_CENTER);
                document.add(paragraph);
                paragraph.setLeading(30f);
                document.add(new Paragraph("Date :" + new DateTime(commande.getDate()).toString("dd/MM/YYYY")));

                paragraph = new Paragraph("PlastProd");
                paragraph.setLeading(30f);
                document.add(paragraph);
                document.add(new Paragraph("Zone d'activité Nord"));
                document.add(new Paragraph("76000 Rouen"));

                AdresseServices srvAdr = new AdresseServices();
                Adresse adresse = srvAdr.get(commande.getIdAdresseFacturation());

                paragraph = new Paragraph(client.getNom());
                paragraph.setAlignment(Element.ALIGN_RIGHT);
                paragraph.setLeading(30f);
                document.add(paragraph);

                paragraph = new Paragraph(adresse.getNumVoie() + " " + adresse.getNomVoie());
                paragraph.setAlignment(Element.ALIGN_RIGHT);
                document.add(paragraph);

                if (null != adresse.getComplement()) {
                    paragraph = new Paragraph(adresse.getComplement());
                    paragraph.setAlignment(Element.ALIGN_RIGHT);
                    document.add(paragraph);
                }
                paragraph = new Paragraph(adresse.getCodePostal() + " " + adresse.getVille());
                paragraph.setAlignment(Element.ALIGN_RIGHT);
                document.add(paragraph);

                paragraph = new Paragraph(adresse.getPays());
                paragraph.setAlignment(Element.ALIGN_RIGHT);
                document.add(paragraph);

                document.add(tableau);
                paragraph = new Paragraph("Prix total : " + ((double)Math.round(prixTotal * 100) / 100));
                paragraph.setAlignment(Element.ALIGN_RIGHT);
                document.add(paragraph);
            } catch (DocumentException de) {
                de.printStackTrace();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }

            document.close();
            try {
                Desktop.getDesktop().open(new File("c:/temp/test.pdf"));
            } catch (IOException ex) {
                Logger.getLogger(FicheClient.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButtonImprimerCommandeActionPerformed

    private void jButtonNoterCommandeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNoterCommandeActionPerformed
        JOptionPane jop = new JOptionPane();
        CommandeServices srvCom = new CommandeServices();
        if (0 > jTableCommandes.getSelectedRow()) {
            jop.showMessageDialog(this, "Vous n'avez pas sélectionné de commande");
        } else {
            int row = jTableCommandes.getSelectedRow();
            Commande commande = (Commande) jTableCommandes.getValueAt(row, 1);
//            if ("expédiée" != commande.getLibelleEtatCommande()) {
//                jop.showMessageDialog(this, "Cette commande n'est pas encore terminé");
//            } else if (null != srvCom.getSatisfactions(commande).get(0)) {
            if (!srvCom.getSatisfactions(commande).isEmpty()) {
                jop.showMessageDialog(this, "Cette commande est déjà noté");
            } else {
                FicheSatisfaction fs = new FicheSatisfaction(this.welcome, this, commande);
                fs.setVisible(true);
            }
        }
    }//GEN-LAST:event_jButtonNoterCommandeActionPerformed

    private void jButtonVoirCommandeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVoirCommandeActionPerformed
        if (0 > jTableCommandes.getSelectedRow()) {
            JOptionPane.showMessageDialog(this, "Vous n'avez pas sélectionné de commande");
        } else {
            int row = jTableCommandes.getSelectedRow();
            Commande commande = (Commande) jTableCommandes.getValueAt(row, 1);
            FicheDetailDemande dd = new FicheDetailDemande(this, client, null, commande);
            dd.setVisible(true);
        }
    }//GEN-LAST:event_jButtonVoirCommandeActionPerformed

    public JTextField getTxtNomClient() {
        return txtNomClient;
    }

    public Contact getContact() {
        return contact;
    }

    public Adresse getAdresse() {
        return adresse;
    }

    private void addCellHeader(Table tableau, Cell cell) {
        cell.setHeader(true);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        tableau.addCell(cell);
    }

    private void addCell(Table tableau, Cell cell) {
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        tableau.addCell(cell);
    }
    
    public javax.swing.JTable getJTableDemandes(){
        return JTableDemandes;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable JTableDemandes;
    private javax.swing.JButton btnAjoutDemande;
    private javax.swing.JButton btnAjouterAdresse;
    private javax.swing.JButton btnAjouterContact;
    private javax.swing.JButton btnModifierAdresse;
    private javax.swing.JButton btnModifierClient;
    private javax.swing.JButton btnModifierContact;
    private javax.swing.JButton btnSauvegarderClient;
    private javax.swing.JButton btnSupprimerAdresse;
    private javax.swing.JButton btnSupprimerClient;
    private javax.swing.JButton btnSupprimerContact;
    private javax.swing.JButton jButtonImprimerCommande;
    private javax.swing.JButton jButtonNoterCommande;
    private javax.swing.JButton jButtonVoirCommande;
    private javax.swing.JButton jButtonVoirDemande;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTable jTableAdresses;
    private javax.swing.JTable jTableCommandes;
    private javax.swing.JTable jTableContacts;
    private javax.swing.JTextField txtNomClient;
    // End of variables declaration//GEN-END:variables
}
